package org.ykh.gmall.item.service.impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ykh.gamll.feign.list.client.ListFeignClient;
import org.ykh.gmall.common.service.constant.RedisConst;
import org.ykh.gmall.feign.product.client.ProductFeignClient;
import org.ykh.gmall.item.service.ItemService;
import org.ykh.gmall.product.*;
import org.ykh.service.exception.BusinessException;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor executor;


    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 商品详情查询
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> item(Long skuId) {
        //创建封装对象
        Map<String, Object> resultMap = new HashMap<>();

//        //判断是否存在
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
//        //判断-如果不存在则直接返回
//        if(!bloomFilter.contains(skuId)){
//
//            return resultMap;
//        }


        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {

            //查询skuInfo
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);

            resultMap.put("skuInfo", skuInfo);

            return skuInfo;

        }, executor);


        //查询三级分类
        CompletableFuture<Void> categoryViewFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {

            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            resultMap.put("categoryView", categoryView);

        }, executor);


        CompletableFuture<Void> findSpuPosterFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
            resultMap.put("spuPosterList", spuPosterList);
        }, executor);

        CompletableFuture<Void> spuSaleAttrListFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            resultMap.put("spuSaleAttrList", spuSaleAttrList);
        }, executor);

        CompletableFuture<Void> skuValueIdsMapFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
//            resultMap.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
            try {
                resultMap.put("valuesSkuJson", objectMapper.writeValueAsString(skuValueIdsMap));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                throw new BusinessException(e);
            }
        }, executor);


        CompletableFuture<Void> attrListFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);


            //处理平台属性
            List<Map<String, Object>> attrListMap = attrList.stream().map(baseAttrInfo -> {

                Map<String, Object> attrMap = new HashMap<>();
                //封装属性名称
                attrMap.put("attrName", baseAttrInfo.getAttrName());
                //封装属性值
                attrMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());

                return attrMap;

            }).collect(Collectors.toList());

            resultMap.put("skuAttrList", attrListMap);

        }, executor);


        CompletableFuture<Void> skuPriceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            resultMap.put("price", skuPrice);

        }, executor);


        //热度统计
        CompletableFuture<Void> incrHotScoreFuture = CompletableFuture.runAsync(() -> {

            listFeignClient.incrHotScore(skuId);
        }, executor);

        //组合编排
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewFuture,
                spuSaleAttrListFuture,
                attrListFuture,
                skuPriceFuture,
                findSpuPosterFuture,
                skuValueIdsMapFuture
        ).join();
        return resultMap;
    }
}
